/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bhatni.mockprocessor.expression;

import com.bhatni.mockprocessor.Message;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Viren
 */
public class RegexConditionExpression extends BaseSingleConditionExpression{
 
    private Pattern pattern;

    @Override
    public boolean evaluate(Message in) {
        String fact;
        if (getFactType() == FACT_TYPE.HEADER)
            fact = in.getHeader(getFactFilter()).toString();
        else
            fact = in.getBody().toString();

        Matcher matcher = getPattern().matcher(fact);
        if (matcher.matches()) {
            
            return true;
        } else {
            return false;
        }


    }
    /**
     * @param expression the expression to set
     */
    @Override
    public void setExpression(String expression) {
        super.setExpression(expression);
        this.setPattern(Pattern.compile(expression));
    }


    /**
     * @return the pattern
     */
    public Pattern getPattern() {
        return pattern;
    }

    /**
     * @param pattern the pattern to set
     */
    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

}
